//
//  USSDInfoVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/21.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class USSDInfoVC: RootVC {
    
    private lazy var chatModel: ChatModel = {
        let model = ChatModel()
        model.isGroupChat = false
        return model
    }()
    
    private lazy var bgView: UIScrollView = {
        let view = UIScrollView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - naviView!.bottom))
        view.backgroundColor = whiteColor
        view.contentSize = view.size
        view.isScrollEnabled = false
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight - naviView!.bottom - inputV.height), style: .grouped)
        tab.backgroundColor = bgView.backgroundColor
        tab.rowHeight = 50
        tab.register(UUMessageCell.self, forCellReuseIdentifier: "cell")
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        return tab
    }()
    
    private lazy var inputV: UUInputFunctionView = {
        let view = UUInputFunctionView(frame: CGRect(x: 0, y: phoneBottom - naviView!.bottom - 60 , width: screenWidth, height: 60))
        view.delegate = self
        view.backgroundColor = whiteColor
        return view
    }()
    
    override func configView() {
        configNaviView(title: "USSD")
        
        view.addSubview(bgView)
        bgView.addSubview(tableView)
        bgView.addSubview(inputV)
        tableView.reloadData()
        
        net_getUSSDFlag { (success) in
            
        }
    }
    
    private func sendUSSD(number:String) {
        net_sendUSSD(number: number) { (success) in
            
        }
    }
    
}

extension USSDInfoVC : UITableViewDataSource,
UITableViewDelegate ,UUInputFunctionViewDelegate{
    
    private func tableViewScrollToBottom() {
        guard chatModel.dataSource.count > 0 else {
            return
        }
        let indexPath = IndexPath(row: chatModel.dataSource.count-1, section: 0)
        tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
    }
    
    private func dealTheFunctionData(dic:[String:Any]) {
        chatModel.addSpecifiedItem(dic);
        tableView.reloadData()
        tableViewScrollToBottom()
    }
    
    func uuInputFunctionView(_ funcView: UUInputFunctionView!, sendMessage message: String!) {
        sendUSSD(number: message)
//        let dic : [String : Any] = ["strContent":message,"type":0]
//        funcView.textViewInput.text = "";
//        dealTheFunctionData(dic: dic)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return chatModel.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UUMessageCell
        cell.messageFrame = chatModel.dataSource[indexPath.row] as! UUMessageFrame
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return (chatModel.dataSource[indexPath.row] as! UUMessageFrame).cellHeight
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
